package com.example.book.controller.admin;

import com.example.book.context.BaseContext;
import com.example.book.dto.AdminDTO;
import com.example.book.dto.BookDTO;
import com.example.book.dto.BookPageDTO;
import com.example.book.dto.UserPageDTO;
import com.example.book.entity.Book;
import com.example.book.result.PageResult;
import com.example.book.service.impl.admin.AdminService;
import com.example.book.service.impl.admin.BookService;
import com.example.book.service.impl.user.UserService;
import com.example.book.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.AccountNotFoundException;
import java.util.List;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private BookService bookService;
    @Autowired
    private TokenUtil tokenUtil;
    //登录
    @PostMapping("/login")
    public String login(@RequestBody AdminDTO adminDTO) throws AccountNotFoundException {
        log.info("登陆人的信息：{}",adminDTO);
        String role = "ROLE_ADMIN";
        String token = tokenUtil.getAdminToken(adminDTO.name,role);
        adminService.login(adminDTO);
        return "AdminToken:"+token;
    }
    //分页查询书籍
    @GetMapping("/page")
    public List<PageResult> page(BookPageDTO bookPageDTO){
        log.info("要请求的页码数据：{}",bookPageDTO);
        PageResult pageResult = bookService.pageInquire(bookPageDTO);
        return pageResult.getRecords();
    }
    //新增书籍
    @PostMapping("/insert")
    public String insertBook(@RequestBody BookDTO bookDTO) throws AccountNotFoundException {
        log.info("要新增的书籍信息：{}",bookDTO);
        bookService.insertBook(bookDTO);
        return "新增书籍成功";
    }
    //批量删除图书
    @DeleteMapping("/delete")
    public String delete(@RequestParam List<String> bookName) throws AccountNotFoundException {
        log.info("要删除的书籍名为：{}",bookName);
        bookService.deleteByBookName(bookName);
        return "删除成功";
    }
    //查询用户信息
    @GetMapping("/UserPage")
    public List<PageResult> page(UserPageDTO userPageDTO){
        log.info("要请求的页码数据：{}",userPageDTO);
        PageResult pageResult = adminService.UserPage(userPageDTO);
        return pageResult.getRecords();
    }
    //批量删除用户
    @DeleteMapping("/deleteUser")
    public String deleteUser(@RequestParam List<String> name){
        log.info("要删除的用户为：{}",name);
        adminService.deleteUserByName(name);
        return "删除成功";
    }
}

